package com.zb.frame.modules.sys.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.frame.base.model.MPPage;
import com.zb.frame.base.model.R;
import com.zb.frame.modules.common.controller.AbstractController;
import com.zb.frame.modules.monitor.enums.LogModule;
import com.zb.frame.modules.monitor.enums.LogType;
import com.zb.frame.modules.monitor.service.LogService;
import com.zb.frame.modules.sys.entity.ParamType;
import com.zb.frame.modules.sys.entity.Role;
import com.zb.frame.modules.sys.service.ParamListService;
import com.zb.frame.modules.sys.service.ParamTypeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bangZZ
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/sys/param/type")
public class ParamTypeController extends AbstractController {

    @Autowired
    ParamTypeService paramTypeService;

    @Autowired
    ParamListService paramService;

    @Autowired
    LogService logService;

    @ApiOperation(value = "getParamTypes", notes = "获取所有参数类型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "一页显示数量", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "参数类型名称/别名", dataType = "String")
    })
    @GetMapping("/list")
    @RequiresPermissions("sys:param:view")
    public R getParamTypes(Integer page, Integer limit, String name) {
        Integer currPage = (page == null) ? 1 : page;
        Integer pageSize = (limit == null) ? 10 : limit;
        Page<ParamType> pageInfo = new Page<>(currPage, pageSize);
        Map<String, Object> query = new HashMap<>();
        query.put("name", name);
        MPPage retData = paramTypeService.getParamTypeList(pageInfo, query);
        return R.ok(retData);
    }

    @ApiOperation(value = "getParamType", notes = "根据参数类型Id获取该参数类型信息")
    @ApiImplicitParam(name = "id", value = "参数类型Id", dataType = "String")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:role:view")
    public R getParamType(@PathVariable Long id) {
        ParamType paramType = paramTypeService.getById(id);
        return (paramType != null) ? R.ok(paramType) : R.error(404, "","失败，参数类型不存在！");
    }

    @ApiOperation(value = "newParamType", notes = "新增参数类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paName", value = "参数类型名称", dataType = "String"),
            @ApiImplicitParam(name = "paAlias", value = "参数类型别名", dataType = "String"),
    })
    @PostMapping("/info")
    @RequiresPermissions("sys:param:add")
    public R newRole(@RequestBody ParamType data) {
        String paramName = data.getPaName();
        boolean isExist = paramTypeService.isExist(paramName);
        if (isExist)
            return R.error("失败，参数类型已存在!");
        ParamType paramType = paramTypeService.addParamType(data);
        if (paramType != null) {
            // 添加日志
            String newValue = JSON.toJSONString(paramType);
            logService.addLog(LogModule.SYSPARAM, LogType.ADD,"", newValue, String.valueOf(paramType.getPaId()), this.getUserId(),"新增参数类型[" + paramType.getPaName() + "]成功");
            return R.ok("成功！",paramType.getPaId());
        } else {
            return R.error("失败!");
        }
    }

    @ApiOperation(value = "updateParamType", notes = "修改参数类型")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paAlias", value = "参数类型别名", dataType = "String"),
    })
    @PutMapping("/info/{id}")
    @RequiresPermissions("sys:param:edit")
    public R updateRole(@PathVariable String id, @RequestBody ParamType data) {
        String oldValue = JSON.toJSONString(data);
        ParamType newParamType = paramTypeService.updateParamType(data);
        if (newParamType == null)
            return R.error("失败,参数类型不存在！");
        String newValue = JSON.toJSONString(newParamType);
        // 添加日志
        logService.addLog(LogModule.SYSPARAM, LogType.UPDATE, oldValue, newValue, String.valueOf(newParamType.getPaId()), this.getUserId(), "修改角色[" + newParamType.getPaId()+ "]成功");
        return R.ok("成功！");
    }

    @ApiOperation(value = "deleteParamType", notes = "删除参数类型")
    @ApiImplicitParam(name = "roleId", value = "角色主键", dataType = "String")
    @DeleteMapping("/info/{id}")
    @RequiresPermissions("sys:role:del")
    public R deleteParamType(@PathVariable String id){
        ParamType paramType = paramTypeService.getById(id);
        if (paramType == null) {
            return R.error(404, "","失败，参数类型不存在！");
        }
        if (paramType.getIsSys() == 1) {
            return R.error("失败，系统参数不可删除！");
        }
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("type_id",paramType.getPaId());
        paramService.removeByMap(columnMap);
        paramTypeService.removeById(id);
        // 添加日志
        String oldValue = JSON.toJSONString(paramType);
        logService.addLog(LogModule.SYSPARAM, LogType.DEL, oldValue, "", String.valueOf(paramType.getPaId()), this.getUserId(), "删除参数类型[" + paramType.getPaName() + "]及其下参数成功");
        return R.ok("成功！");
    }

}
